<template>
    <div class="app-container">
      <header-title/>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="关键词" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入关键词"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="el-icon-add" @click="onHandleAdd">新增</el-button>

        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="tableList">
        <el-table-column label="ID" align="center" prop="lbtId" width="100"/>
        <el-table-column label="图片" align="center">
          <template slot-scope="scope">
            <imagePreview :url="scope.row.lbtUrl"/>
          </template>
        </el-table-column>
        <el-table-column label="跳转链接" align="center">
          <template slot-scope="scope">
            {{ scope.row.redirectUrl || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="是否需要登录" align="center">
          <template slot-scope="scope">
            {{ scope.row.isLogin == 0 ? '否' : '是' }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="onHandleClickDel(scope.row.lbtId)"
        >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="90px">
                <el-row :gutter="20">
                    <el-col :span="24" >
                        <el-form-item  label="轮播图图片" prop="lbtUrl">
                            <imageUpload v-model="form.lbtUrl" size="mini" :limit="1" :isJson="false"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="跳转路径" prop="redirectUrl">
                            <el-input  v-model="form.redirectUrl" placeholder="请输入跳转路径"  />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="是否登录" prop="isLogin">
                            <el-radio-group v-model="form.isLogin">
                                <el-radio :label="0" :value="0">否</el-radio>
                                <el-radio :label="1" :value="1">是</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
  </template>
  
  <script>
  import { listPosterImage, addPosterImage,updatePosterImage,delPosterImage } from "@/api/operateManage/lbt";
  import { getToken } from "@/utils/auth";
  export default {
    name: "PosterImage",
    data() {
      return {
        uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        tableList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          keyword: undefined,
          type:1
        //   lbt_type: 1
        },
        // 表单参数
        form: {isLogin: 0,type: 1},
        // 表单校验
        rules: {
            lbtUrl: [
                { required: true, message: "图片不能为空", trigger: "change" }
            ],
            redirectUrl: [
                { required: true, message: "跳转路径不能为空", trigger: "change" }
            ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
        onHandleClickDel(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delPosterImage(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })
                
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        onHandleAdd() {
          this.detailId = ''
            this.title = '新增轮播图'
            this.open = true
        },
        /** 查询参数列表 */
        getList() {
            this.loading = true;
            console.log(this.queryParams,'=')
            listPosterImage(this.queryParams).then(response => {
                this.tableList = response.data.records;
                this.total = response.data.total;
                this.loading = false;
            }
            );
        },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          name: undefined,
          sortNum: 0
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = JSON.parse(JSON.stringify(row));
        this.open = true;
        this.title = "修改轮播图";
      },
      handleUploadSuccess(response, file, fileList) {
        this.form.url = response.url
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if(this.form.lbtId) {
                updatePosterImage(this.form).then(response => {
                    this.$modal.msgSuccess("更新轮播图成功");
                    this.open = false;
                    this.getList();
                });
            }else{
                addPosterImage(this.form).then(response => {
                    this.$modal.msgSuccess("新增轮播图成功");
                    this.open = false;
                    this.getList();
                });
            }
            
          }
        });
      }
    }
  };
  </script>
  <style lang="scss">
  .poster-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover:after{
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: rgba(0,0,0,0.5);
      color: #fff;
      font-size: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: "element-icons" !important;
    }
  }
  
  .poster-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  
  .poster-uploader-icon {
    font-size: 60px;
    color: #8c939d;
    text-align: center;
  }
  
  .poster {
    display: block;
  }
  </style>
  